<template>
  <div class="m-show-classify">
    <div class="add-btn">
      <w-button type="option" @on-click="onUpdateClassify('add', '')">新增</w-button>
    </div>
    <div class="clearfix">
      <w-table :columns="columns" :has-data="hasData">
        <tr v-for="(item, index) in classifyList.recordList" :key="index">
          <td>
            <span>{{(paramsInfo.currentPage - 1) * 20 + (index + 1)}}</span>
          </td>
          <td>
            <span>{{item.showName}}</span>
          </td>
          <td>
            <span>{{item.showType}}</span>
          </td>
          <td>
            <span>{{item.className}}</span>
          </td>
          <!-- <td>
            <span>是</span>
          </td> -->
          <td>
            <div class="options">
              <div class="edit" @click="onUpdateClassify('edit', item.id)">
                <span class="icon-edit"></span>
                <span>修改</span>
              </div>
              <div class="del" @click="onDelete(item.id)">
                <span class="icon-del"></span>
                <span>删除</span>
              </div>
            </div>
          </td>
        </tr>
      </w-table>

      <div style="margin-top: 20px" class="fr" v-if="classifyList.pageCount > 1">
        <w-paging :current="classifyList.currentPage" :total="classifyList.pageCount" @on-page="onChangePage"></w-paging>
      </div>
    </div>

    <!-- 新增展示分类 -->
    <w-panel-validate v-model="displayPanel" :height="`600px`" confirm-text="提交" :panel-title="true" :title="panelTitle" @on-confirm="onComfirm" @on-cancel="_onCancel">
      <div class="panel-classify">
        <div>
          <span class="title">展示名称</span>
          <div class="name">
            <w-input rule="isEmpty" error="请输入" v-model="paramsAdd.showName"></w-input>
          </div>
        </div>
        <div>
          <span class="title">展示终端</span>
          <div class="name">
            <w-select :options="showTypeList" v-model="paramsAdd.showType"></w-select>
          </div>
        </div>
        <div>
          <span class="title">通用分类</span>
          <div class="name">
            <w-select :options="commonList" v-model="paramsAdd.classId"></w-select>
          </div>
        </div>
        <div>
          <span class="title" style="vertical-align: top;">显示</span>
          <div class="name">
            <w-switch v-model="paramsAdd.display" @on-change="onChangeSwitch"></w-switch>
          </div>
        </div>
        <div class="m-upload">
          <div class="keywords">
            <span class="title" :class="{'k-title': paramsAdd.keysInfo && paramsAdd.keysInfo.length && paramsAdd.keysInfo[0].keyword}">关键字</span>
            <div style="width: 251px;">
              <div class="key-list" v-for="(key, keyIndex) in paramsAdd.keysInfo" :key="keyIndex">
                <div class="name" style="width: 130px">
                  <w-input rule="isEmpty" error="请输入" v-model="key.keyword"></w-input>
                </div>
                <div class="upload">
                  <div style="display: inline-block;">
                    <!-- 关键字图片 -->
                    <div class="key-img" v-if="key.img">
                      <img :src="`${key.img}`" alt="">
                    </div>
                    <div class="m-img-upload">
                      <span class="cur t-under">上传图片</span>
                      <input type="file" class="upload-file" accept="image/png,image/jpg,image/jpeg,image/gif" @change="onUploadImg($event, keyIndex)">
                    </div>
                  </div>
                  <div class="delete" @click="paramsAdd.keysInfo.splice(keyIndex, 1)" v-if="keyIndex != 0">
                    <img src="../../assets/delete-big.png" alt="">
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

         <!--新增关键字  -->
        <div class="add-new">
          <w-button type="option" @on-click="paramsAdd.keysInfo.push({'keyword': '', 'img': ''})">新增关键字</w-button>
        </div>
      </div>
    </w-panel-validate>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { apiClassifyMange } from 'js/api'

export default {
  data() {
    return {
      columns: [
        {title: '排序', width: '8%'},
        {title: '展示名称', width: '16%'},
        {title: '展示终端', width: '16%'},
        {title: '通用分类', width: '16%'},
        // {title: '是否显示', width: '16%'},
        {title: '操作', width: '24%'}
      ],
      displayPanel: false, // 新增展示分类弹框
      panelTitle: '新增', // 弹框标题
      paramsInfo: {
        accessToken: this.$store.getters.token || '',
        threeMenuCode: this.$route.query.three || '',
        currentPage: 1,
        showCount: 20
      },
      classifyList: {},
      // 新增，更新
      paramsAdd: {
        accessToken: this.$store.getters.token || '',
        threeMenuCode: this.$route.query.three || '',
        showName: '',
        showType: '',
        classId: '',
        // 弹框--关键字的数据列表
        keysInfo: [
          {
            'keyword': '',
            'img': ''
          }
        ],
        display: true
      },
      // 展示终端
      showTypeList: [
        {
          label: 'Pc',
          value: 'Pc'
        },
        {
          label: 'Wap',
          value: 'Wap'
        }
      ],
      commonList: [] // 展示分类
    }
  },
  computed: {
    ...mapGetters(['token']),
    // 判断是否有数据
    hasData() {
      if (!this.classifyList.recordList || this.classifyList.recordList.length == 0) {
        return false
      } else {
        return true
      }
    }
  },
  created() {
    this.fetchTypeClassify()
    this.fetchCommonClassify()
  },
  methods: {
    /**
     * 查询展示分类
     */
    fetchTypeClassify() {
      apiClassifyMange.apiTypeClassify(this.paramsInfo).then(data => {
        this.classifyList = data.data || ''
      })
    },
    /**
     * 获取通用分类列表
     */
    fetchCommonClassify() {
      const params = {
        'accessToken': this.token,
        'threeMenuCode': this.$route.query.three || '',
        'type': 'all'
      }
      apiClassifyMange.apiCommonClassify(params).then(data => {
        this.commonList = data.data || [] // 通用分类数据
        this.commonList = this.commonList.map(item => {
          return {
            label: item.className,
            value: item.id
          }
        })
      })
    },
    /**
     * 新增--上传关键字图片
     */
    onUploadImg(ev, index) {
      const oFile = ev.target.files[0]
      const formData = new FormData()
      if (!oFile) {
        return
      }
      if (oFile.size > 1048576) {
        this.$toast('请选择小于1M的图片')
        return
      }
      if (!/\.(png|jpeg|jpg|gif)/i.test(oFile.name)) {
        this.$toast('请选择JPG,JPEG,PNG,GIF格式的图片')
        return
      }
      formData.append('file', oFile)
      formData.append('type', 'h5')
      apiClassifyMange.apiUploadImg(formData).then(data => {
        this.paramsAdd.keysInfo[index].img = data.data.filePath
        // this.filePath = data.data.filePath // 图片上传的ip
      })
    },
    /**
    * 展示新增/修改弹框
     */
    onUpdateClassify(type, id) {
      if (type == 'edit' && id) {
        this.panelTitle = '修改'
        apiClassifyMange.apiTypeClassifyUpdated(this.token, this.$route.query.three, id).then(data => {
          this.paramsAdd = Object.assign({}, this.paramsAdd, data.data)
          this.paramsAdd.keysInfo = this.paramsAdd.keysInfos || [{'keyword': '', 'img': ''}]
          // this.filePath = data.data.filePath
        })
      } else {
        this.panelTitle = '新增'
        // 清空数据
        this.paramsAdd = {
          accessToken: this.$store.getters.token || '',
          threeMenuCode: this.$route.query.three || '',
          showName: '',
          showType: '',
          classId: '',
          // 弹框--关键字的数据列表
          keysInfo: [
            {
              'keyword': '',
              'img': ''
            }
          ],
          display: true
        }
      }
      this.displayPanel = true
    },
    /**
     * 新增
     */
    onComfirm() {
      if (!this.paramsAdd.showType) {
        this.$toast('请选择展示终端')
        return
      }
      if (!this.paramsAdd.classId) {
        this.$toast('请选择通用分类')
        return
      }
      if (this.paramsAdd.keysInfo.length == 0 || !this.paramsAdd.keysInfo[0].img) {
        this.$toast('第一个关键字必须上传图片')
        return
      }
      apiClassifyMange.apiTypeClassifyUpdate(this.paramsAdd).then(data => {
        this.$toast(`${this.panelTitle}成功`, () => {
          this.fetchTypeClassify()
          this.displayPanel = false
        })
      })
    },
    /**
     * 清空弹出框数据
     */
    _onCancel() {
      this.paramsAdd = {
        accessToken: this.$store.getters.token || '',
        threeMenuCode: this.$route.query.three || '',
        showName: '',
        showType: '',
        classId: '',
        // 弹框--关键字的数据列表
        keysInfo: [
          {
            'keyword': '',
            'img': ''
          }
        ],
        display: true
      }
    },
    /**
     * 删除
     */
    onDelete(id) {
      let _this = this
      const ids = []
      ids.push(id)
      this.$modal({
        title: '提示',
        content: '确认删除？',
        onConfirm() {
          apiClassifyMange.apiTypeClassifyDel(_this.token, _this.$route.query.three, ids).then(data => {
            _this.$toast('删除成功', () => {
              _this.fetchTypeClassify()
            })
          })
        }
      })
    },
    /**
     * 分页
     */
    onChangePage(page) {
      this.paramsInfo.currentPage = page
      this.fetchTypeClassify()
    },
    /**
     * radio--切换显隐操作
     */
    onChangeSwitch(choice) {
      if (choice == 'false') {
        this.paramsAdd.display = false
      } else {
        this.paramsAdd.display = true
      }
    }
  }
}
</script>

<style lang="scss">
.m-show-classify {
  .add-btn {
    .btn {
      width: 70px;
      height: 28px;
      border: 1px solid #9E9E9E;
      font-size: 12px;
    }
  }
  .table {
    border: 1px solid #DBDBDB;
    td {
      height: 50px;
    }
  }
  .add-new {
    .btn {
      width: 70px;
      font-size: 10px;
    }
  }
}
</style>


<style lang="scss" scoped>
.t-under {
  text-decoration: underline;
  margin-right: 18px;
  font-size: 10px;
  cursor: pointer;
  color: rgba(74,144,226,1);
}
.add-btn {
  margin: 20px 0 10px;
}
.options {
  display: inline-block;
  text-align: center;
  color: #2D8EFE;
  & > div {
    display: inline-block;
    margin: 0 20px;
    cursor: pointer;
  }
  
  .icon-edit {
    width: 12px;
    height: 12px;
    vertical-align: sub;
    background: url(../../assets/icon-edit.png) no-repeat;
    display: inline-block;
    cursor: pointer;
  }
  .icon-del {
    display: inline-block;
    width: 12px;
    height: 12px;
    vertical-align: sub;
    background: url(../../assets/del-bush.png) no-repeat;
    cursor: pointer;
  }
}
.panel-classify {
  width: 340px;
  margin: 0 auto;
  & > div {
    margin-bottom: 20px;
  }
  .title {
    display: inline-block;
    width: 60px;
    text-align: left;
    font-size: 12px;
  }
  .name {
    display: inline-block;
    width: 230px;
  }
}
.m-upload {
  display: flex;
}
.upload {
  width: 96px;
  display: inline-block;
  position: relative;
  margin-left: 25px;
  .key-img {
    width: 50px;
    height: 50px;
    border: 1px solid
  }
  .m-img-upload {
    position: relative;
    input {
      width: 60px;
      position: absolute;
      left: 0;
      overflow: hidden;
      opacity: 0;
      top: 0;
      cursor: pointer;
    }
  }
  .delete {
    width: 16px;
    height: 20px;
    display: inline-block;
    position: absolute;
    top: calc(36% - 10px);
    cursor: pointer;
  }     
}
.keywords {
  // margin-right: 25px;
  display: inline-flex;
  justify-content: center;
  // align-items: center;
  .k-title {
    margin-top:25px;
  }
  .key-list {
    align-items: center;
    justify-content: center;
    display: inline-flex;
    margin-bottom: 5px;
  }
}
.add-new {
  margin-left: 70px;
}
</style>

